package top.cbug.mqtt_yw_aidl_demo_v2.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import bhygzs.cn.mqtt_client.Imqttbgret;
import bhygzs.cn.mqtt_client.Imqttbgset;
import bhygzs.cn.mqtt_client.Imqttbgset2;
import top.cbug.mqtt_yw_aidl_demo_v2.R;

public class MQTT2Activity extends AppCompatActivity {

    /**
     * MQTT_yw_Aidl示例代码2，适用采用用户名/密码认证的MQTT通信连接。
     */

    private Imqttbgset2 imqttbgset;
    private static final String TAG = "MQTT1Activity";
    private TextView tv1;
    private EditText et1, et2, et3;
    private Button btlj, btdk, btdy, btfs;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mqtt2);
        initVIew();
        onBind();

        btlj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    imqttbgset.connect("tcp://mqtt.cbug.top:1883", "cbugclinet2",
                        "用户名","密码",new MQTT2Activity.callback());
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(TAG, "onServiceConnected: " + e);
                }
            }
        });

        btdk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    imqttbgset.disconnect();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });

        btdy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    imqttbgset.set_retopic(et1.getText().toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        btfs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    imqttbgset.fs_toserver(et2.getText().toString(),et3.getText().toString());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void initVIew() {
        tv1 =findViewById(R.id.mqtt2_tv);
        et1 =findViewById(R.id.mqtt2_et1);
        et2 =findViewById(R.id.mqtt2_et2);
        et3 =findViewById(R.id.mqtt2_et3);
        btlj=findViewById(R.id.mqtt2_btlj);
        btdk=findViewById(R.id.mqtt2_btdk);
        btdy=findViewById(R.id.mqtt2_bt1);
        btfs=findViewById(R.id.mqtt2_bt2);
    }

    /***
     * 绑定MQTT_yw的Aidl服务*核心部分
     */
    private void onBind() {
        Intent intent = new Intent();
        intent.setAction("bhygzs.cn.mqtt_client.AIDL2");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setPackage("bhygzs.cn.mqtt_client");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        bindService(intent, new MQTT2Activity.AidlConnection(), BIND_AUTO_CREATE);
    }

    private class AidlConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            imqttbgset = Imqttbgset2.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

    private class callback extends Imqttbgret.Stub {

        @Override
        public void retServermsg(String ss) throws RemoteException {
            tv1.setText(String.format("【服务器消息：{%s}】", ss));
        }

        @Override
        public void retTopicmsg(String ss) throws RemoteException {
            tv1.setText(String.format("【订阅消息：{%s}】", ss));
        }
    }
}